import { Injectable } from '@angular/core'
import { OnInit, OnDestroy } from '@angular/core'
import { IQRRouteOfStore } from '@shared/interfaces/store'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { EPaymentBrand, ECustomAcl } from '@shared/enums'
import { CustomACLService } from '@shared/services/acl.service'
import { PageService } from '@shared/services/page.service'
import { I18NService } from '@core'
@Injectable({
  providedIn: 'root',
})
export class TabQRChannelsService implements OnInit, OnDestroy {
  constructor(
    private pageService: PageService,
    private customACLService: CustomACLService,
    private i18n: I18NService,
  ) {}

  ngOnInit(): void {
    this.initialData()
  }

  ngOnDestroy(): void {
    this.customACLService.removeACLAbility(ECustomAcl.StoreEditable)
    this.customACLService.removeACLAbility(ECustomAcl.StoreTrueMoney)
    this.customACLService.removeACLAbility(ECustomAcl.StoreCurrencies)
  }

  initialData() {
    const isStoreLocked = this.pageService.isStoreLocked
    const isStoreDisplayInTask = this.pageService.isStoreDisplayInTask
    if (!isStoreDisplayInTask && !isStoreLocked) {
      this.customACLService.addACLAbility(ECustomAcl.StoreEditable)
    }
  }

  getChannelColumns(channel: IQRRouteOfStore) {
    switch (channel.paymentBrand) {
      case EPaymentBrand.Alipay:
        return this.getAlipayChannelColumns(channel)
      case EPaymentBrand.WeChat:
        return this.getWeChatChannelColumns(channel)
      case EPaymentBrand.UPI:
        return this.getUPIChannelColumns(channel)
      case EPaymentBrand.TrueMoney:
        return this.getTrueMoneyChannelColumns(channel)
      // case EPaymentBrand.Visa:
      //   return this.getVisaChannelColumns(channel)
      // case EPaymentBrand.MasterCard:
      //   return this.getMasterCardChannelColumns(channel)
      // case EPaymentBrand.FPS:
      //   return this.getFPSChannelColumns(channel)
      default:
        return this.getAlipayChannelColumns(channel)
    }
  }

  private getAlipayChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 8,
          label: 'Alipay Partner ID',
          value: channel['chanMerParams']['PID'],
        },
        {
          span: 8,
          label: 'Alipay secondary merchant id',
          value: channel['chanMerParams']['secondaryMerchantID'],
        },
        {
          span: 8,
          label: 'Alipay secondary merchant name',
          value: channel['chanMerParams']['secondaryMerchantName'],
        },
      ],
      [
        {
          span: 8,
          label: 'Alipay store id',
          value: channel['chanMerParams']['storeID'],
        },
        {
          span: 8,
          label: 'Alipay store name',
          value: channel['chanMerParams']['storeName'],
        },
        {
          span: 8,
          label: 'Alipay secondary merchant industry',
          value: channel.mcc,
        },
      ],
      [
        {
          span: 8,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 8,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        // {
        //   span: 8,
        //   label: 'Seller ID',
        //   value: channel.inApp && channel.inApp.appID,
        // },
        {
          span: 8,
          label: 'GENERAL.INFO.OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  private getWeChatChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 12,
          label: 'WeChat Merchant ID',
          value: channel['chanMerParams']['merchantID'],
        },
        {
          span: 12,
          label: 'WeChat SubMerchant ID',
          value: channel['chanMerParams']['subMerchantID'],
        },
      ],
      [
        {
          span: 6,
          label: 'WeChat MCC',
          value: channel.mcc as string,
        },
        {
          span: 6,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        {
          span: 6,
          label: 'GENERAL.INFO.OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  // private getTrueMoneyChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
  //   return [
  //     [
  //       {
  //         span: 12,
  //         label: 'TrueMoney SubMerchant ID',
  //         value: channel['chanMerParams']['subMerchantID'],
  //       },
  //       {
  //         span: 12,
  //         label: 'TrueMoney Brand Store ID',
  //         value: channel['chanMerParams']['brandStoreID'],
  //       },
  //     ],
  //     [
  //       {
  //         span: 8,
  //         label: 'Transaction Fee',
  //         value: this.toPercentage(channel.fee),
  //       },
  //       // {
  //       //   span: 8,
  //       //   label: 'Channel transaction currency',
  //       //   value: channel.currency && channel.currency.code,
  //       // },
  //       {
  //         span: 8,
  //         label: 'GENERAL.INFO.OPERATION',
  // labelI18n: 'GENERAL.INFO.OPERATION',
  //         operate: true,
  //         acl: ['api.put.zebra.store.one.channel'],
  //       },
  //     ],
  //   ]
  // }

  private getTrueMoneyChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
    if (channel.chanMerParams.payNearby) {
      this.customACLService.addACLAbility(ECustomAcl.StoreTrueMoney)
    }
    if (channel.currency) {
      this.customACLService.addACLAbility(ECustomAcl.StoreCurrencies)
    }
    return [
      [
        {
          span: 8,
          label: this.trans('STORE.TRURMONEY.CHANNEL.FACE'),
          value: channel.chanMerParams.payNearby ? this.trans('GENERAL.INFO.YES') : this.trans('GENERAL.INFO.NO') ,
        },
        {
          span: 8,
          label: this.trans('STORE.TRURMONEY.CHANNEL.ONLINE'),
          value: channel.chanMerParams.authOnline ? this.trans('GENERAL.INFO.YES') : this.trans('GENERAL.INFO.NO'),
        },
        {
          span: 8,
          label: 'Merchant ID',
          value: channel.chanMerID,
        },
      ],
      [
        {
          span: 6,
          label: 'Shop ID',
          value: channel.chanMerParams.shopID,
          acl: [ECustomAcl.StoreTrueMoney],
        },
        {
          span: 6,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          label: 'Channel transaction currency',
          value: channel.currency,
          acl: [ECustomAcl.StoreCurrencies],
        },
        {
          span: 6,
          label: 'GENERAL.INFO.OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel'],
        },
      ],
    ]
  }
  private getUPIChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
    return [
      [
        {
          span: 12,
          label: 'UPI Acquiring IIN',
          value: channel['chanMerParams']['acquiringIIN'],
        },
        {
          span: 12,
          label: 'UPI Forwarding IIN',
          value: channel['chanMerParams']['forwardingIIN'],
        },
      ],
      [
        {
          span: 6,
          label: 'UPI MCC',
          value: channel.mcc as string,
        },
        {
          span: 6,
          label: 'Transaction Fee',
          value: this.toPercentage(channel.fee),
        },
        {
          span: 6,
          label: 'Channel transaction currency',
          value: channel.currency,
        },
        {
          span: 6,
          label: 'GENERAL.INFO.OPERATION',
          labelI18n: 'GENERAL.INFO.OPERATION',
          operate: true,
          acl: ['api.put.zebra.store.one.channel', ECustomAcl.StoreEditable],
          aclMode: 'allOf',
        },
      ],
    ]
  }

  // private getVisaChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
  //   return [
  //     [
  //       {
  //         span: 16,
  //         label: 'Visa BIN',
  //         value: channel.chanMerID,
  //       },
  //       {
  //         span: 8,
  //         label: 'Visa MCC',
  //         value: channel.mcc,
  //       },
  //     ],
  //     [
  //       {
  //         span: 6,
  //         label: 'Transaction Fee',
  //         value: this.toPercentage(channel.fee),
  //       },
  //       {
  //         span: 6,
  //         label: 'Channel transaction currency',
  //         value: channel.currency && channel.currency.code,
  //       },
  //       {
  //         span: 6,
  //         label: 'GENERAL.INFO.OPERATION',
  // labelI18n: 'GENERAL.INFO.OPERATION',
  //         operate: true,
  //         acl: ['api.put.zebra.store.one.channel'],
  //       },
  //     ],
  //   ]
  // }

  // private getMasterCardChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
  //   return [
  //     [
  //       {
  //         span: 12,
  //         label: 'MasterCard BIN',
  //         value: channel.chanMerID,
  //       },
  //       {
  //         span: 12,
  //         label: 'MasterCard ICA',
  //         value: channel.chanMerIDSecondary,
  //       },
  //     ],
  //     [
  //       {
  //         span: 6,
  //         label: 'MasterCard MCC',
  //         value: channel.mcc,
  //       },
  //       {
  //         span: 6,
  //         label: 'Transaction Fee',
  //         value: this.toPercentage(channel.fee),
  //       },
  //       {
  //         span: 6,
  //         label: 'Channel transaction currency',
  //         value: channel.currency && channel.currency.code,
  //       },
  //       {
  //         span: 6,
  //         label: 'GENERAL.INFO.OPERATION',
  // labelI18n: 'GENERAL.INFO.OPERATION',
  //         operate: true,
  //         acl: ['api.put.zebra.store.one.channel'],
  //       },
  //     ],
  //   ]
  // }

  // private getFPSChannelColumns(channel: IQRRouteOfStore): IBaseInfoColumn[][] {
  //   return [
  //     [
  //       {
  //         span: 8,
  //         label: 'FPS Bank ID',
  //         value: channel.chanMerID,
  //       },
  //       {
  //         span: 8,
  //         label: 'FPS GUID',
  //         value: channel.chanMerIDSecondary,
  //       },
  //       {
  //         span: 8,
  //         label: 'FPS Identifier',
  //         value: channel.chanMerIDTertiary,
  //       },
  //     ],
  //     [
  //       {
  //         span: 6,
  //         label: 'FPS MCC',
  //         value: channel.mcc,
  //       },
  //       {
  //         span: 6,
  //         label: 'Transaction Fee',
  //         value: this.toPercentage(channel.fee),
  //       },
  //       {
  //         span: 6,
  //         label: 'Channel transaction currency',
  //         value: channel.currency && channel.currency.code,
  //       },
  //       {
  //         span: 6,
  //         label: 'GENERAL.INFO.OPERATION',
  // labelI18n: 'GENERAL.INFO.OPERATION',
  //         operate: true,
  //         acl: ['api.put.zebra.store.one.channel'],
  //       },
  //     ],
  //   ]
  // }

  private toPercentage(fee: number): string {
    const per = fee * 100
    return `${per.toFixed(4)}%`
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
